<?php

namespace app\topics\middle;

class DetailManageMiddle extends \app\base\middle\BaseMiddle {


    public $manageIds = [];
    public $info = [];
    public $userId = 0;

    private function commonData() {
        $id = intval($this->params['id']);
        $userId = intval($this->params['user_id']);
        if(empty($id) || empty($userId)) {
            return $this->stop('ID不存在！');
        }
        $this->userId = $userId;
        $info = target('topics/TopicsDetail')->getWhereInfo([
            'A.detail_id' => $id
        ]);
        if (empty($info)) {
            return $this->stop('主题不存在！');
        }
        $nodeInfo = target('topics/TopicsNode')->getWhereInfo([
            'A.node_id' => $info['node_id']
        ]);
        if (empty($nodeInfo)) {
            return $this->stop('节点不存在！');
        }
        $manageUser = explode(',', $this->params['manage_user']);
        $moderator = explode(',', $nodeInfo['moderator']);

        $this->manageIds = array_merge($manageUser, $moderator);
        $this->info = $info;
    }

    /**
     * 删除主题
     */
    protected function del() {
        $this->commonData();
        if($this->info['user_id'] <> $this->userId && !in_array($this->userId, $this->manageIds)) {
            return $this->stop('没有权限删除！');
        }
        target('topics/TopicsDetail')->beginTransaction();
        $status = target('topics/TopicsDetail')->del($this->info['detail_id']);
        if(!$status) {
            return $this->stop(target('topics/TopicsDetail')->getError());
        }
        $type = target('topics/Topics', 'service')->getTypeAll();
        $status = target($type[$this->info['type']]['model'])->where(['detail_id' => $this->info['detail_id']])->delete();
        if(!$status) {
            return $this->stop('删除类型主题失败！');
        }
        $status = target('topics/TopicsDetailReply')->where(['detail_id' => $this->info['detail_id']])->delete();
        if(!$status) {
            return $this->stop('删除回帖失败！');
        }
        $status = target('topics/TopicsSearch')->where(['detail_id' => $this->info['detail_id']])->delete();
        if(!$status) {
            return $this->stop('删除索引失败！');
        }
        target('topics/TopicsDetail')->commit();
        return $this->run([], '删除成功！');
    }

    /**
     * 置顶主题
     */
    protected function top() {
        $this->commonData();
        if(!in_array($this->userId, $this->manageIds)) {
            return $this->stop('没有权限操作！');
        }
        $status = target('topics/TopicsDetail')->edit([
            'detail_id' => $this->info['detail_id'],
            'attr_top' => $this->info['attr_top'] ? 0 : 1,
            'attr_top_time' => $this->info['attr_top'] ? 0 : time()
        ]);
        if(!$status) {
            return $this->stop('置顶操作失败！');
        }

        return $this->run([], $this->info['attr_excellent'] ? '取消置顶主题成功！' : '置顶主题成功！');
    }

    /**
     * 精华主题
     */
    protected function excellent() {
        $this->commonData();
        if(!in_array($this->userId, $this->manageIds)) {
            return $this->stop('没有权限操作！');
        }
        $status = target('topics/TopicsDetail')->edit([
            'detail_id' => $this->info['detail_id'],
            'attr_excellent' => $this->info['attr_excellent'] ? 0 : 1,
            'attr_excellent_time' => $this->info['attr_excellent'] ? 0 : time()
        ]);
        if(!$status) {
            return $this->stop('加精操作失败！');
        }

        return $this->run([],$this->info['attr_excellent'] ? '取消精华主题成功！' : '加精主题成功！');
    }

    /**
     * 删除回复
     */
    protected function reply() {
        $this->commonData();
        if(!in_array($this->userId, $this->manageIds)) {
            return $this->stop('没有权限操作！');
        }
        $replyId = intval($this->params['reply_id']);
        if(empty($replyId)) {
            return $this->stop('回复不存在！');
        }
        $replyInfo = target('topics/TopicsDetailReply')->getWhereInfo([
            'A.reply_id' => $replyId,
            'A.detail_id' => $this->info['detail_id']
        ]);
        if(empty($replyInfo)) {
            return $this->stop('该回复不存在');
        }
        $status = target('topics/TopicsDetailReply')->del($replyId);
        if(!$status) {
            return $this->stop(target('topics/TopicsDetailReply')->getError());
        }

        return $this->run([],'删除回复成功！');
    }


}